package pw.goyd.game.handler.imp;

import org.jboss.netty.channel.ChannelFuture;
import pw.goyd.db.RedisUtil;
import pw.goyd.game.api.NumberUtils;
import pw.goyd.game.constant.setting.GameSetting;
import pw.goyd.game.handler.Handler;
import pw.goyd.game.logic.AddPlayerRoomResponse;
import pw.goyd.game.logic.GetPlayRequest;
import pw.goyd.game.logic.GetPlayResponse;
import pw.goyd.game.manager.RoomManager;
import pw.goyd.game.model.Room;
import pw.goyd.game.server.Log;
import pw.goyd.game.server.LoginList;
import pw.goyd.protocol.model.Response;
import pw.goyd.subscribe.PlayerCustomer;

public class GETPLAY extends Handler {
    /**
     * 匹配游戏
     */
    @Override
    public void handle() {
        Response response = new Response();
        GetPlayRequest getPlayRequest = new GetPlayRequest();
        getPlayRequest.readFromBytes(request.getData());
        int leastScore = getPlayRequest.getLeastScore();

        PlayerCustomer p = LoginList.getPlayer(ctx.getChannel().getId());
        if (p == null) {
            Log.getLogger().error("空指针--->ChannelID[" + ctx.getChannel().getId() + "]");
            return;
        }
        //金币不足 不允许进入房间
        if (p.getGold() < GameSetting.MIN_ROOM_MONEY) {
            //TODO 没有根据房间信息来判断
            p.sendKickOutRoom();    //  金币不够
            return;
        }

        if (p.getRoomID() == -1) {
            // 不在房间内
            Room troom = RoomManager.sitPlayer(p, leastScore);
            Log.getLogger().info(
                    "匹配---->Score:" + leastScore + " Nickname:"
                            + p.getNickname() + " Room:"
                            + troom.getRoomID());
            // 推送给其他人
            AddPlayerRoomResponse add = new AddPlayerRoomResponse();
            add.setPlayer(p);
            Response response2 = new Response();
            response2.setCmd(CMD_ADDPLAYER);
            response2.setData(add.getBytes());
            // 推送订阅给其他人增加的玩家信息
            RedisUtil.pubRoom(troom.getRoomID(), response2.getAllBytes());

            // 推送房间详细包
            GetPlayResponse getPlayResponse = new GetPlayResponse();
            getPlayResponse.setRoom(troom);
            response.setCmd(CMD_GETPLAY);
            response.setData(getPlayResponse.getBytes());
            Log.getLogger().debug(NumberUtils.byteToHexString(response.getAllBytes()));
            ChannelFuture cf = ctx.getChannel().write(response);
            // 当前人订阅房间消息 (客户端需要做判断 因为redis推送出去订阅消息有延迟 导致后进入的玩家也收到自身加入包)
            if (cf.isSuccess())
                p.psubRoom();


        } else {    //  在房间内 断线重连或者网络原因
            Room troom = RoomManager.getRoom(p.getRoomID());
            p.psubRoom();       //  订阅房间消息
            //  进入房间消息
            GetPlayResponse getPlayResponse = new GetPlayResponse();
            getPlayResponse.setRoom(troom);
            response.setCmd(CMD_GETPLAY);
            response.setData(getPlayResponse.getBytes());
            ctx.getChannel().write(response);

            //  移出退房离线消息队列
            if (troom.leaveList.contains(p)) {
                troom.leaveList.remove(p);//移出退出房间队列
                Log.getLogger().info(
                        "游戏中重进---->Score:" + leastScore + " Nickname:"
                                + p.getNickname() + " Room:"
                                + troom.getRoomID());
            } else {
                Log.getLogger().info(
                        "未进入重进---->Score:" + leastScore + " Nickname:"
                                + p.getNickname() + " Room:"
                                + troom.getRoomID());
            }


            //TODO 空指针 待测试
            if (troom.getIsPlayingPlayer().contains(p)) {   // 正在游戏
                if (p.getCards() != null)   //  拥有手牌
                    p.sendCards();          //  发送手牌
                p.getChannel().write(troom.getTimeDownPack());  //  发送倒计时
            }
        }
    }
}
